package srx.open.com.receviers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import org.apache.commons.collections.CollectionUtils;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.im.BmobChatManager;
import cn.bmob.im.BmobNotifyManager;
import cn.bmob.im.BmobUserManager;
import cn.bmob.im.bean.BmobChatUser;
import cn.bmob.im.bean.BmobInvitation;
import cn.bmob.im.bean.BmobMsg;
import cn.bmob.im.config.BmobConfig;
import cn.bmob.im.config.BmobConstant;
import cn.bmob.im.db.BmobDB;
import cn.bmob.im.inteface.EventListener;
import cn.bmob.im.inteface.OnReceiveListener;
import cn.bmob.im.util.BmobJsonUtil;
import cn.bmob.v3.listener.FindListener;
import de.greenrobot.event.EventBus;
import srx.open.com.R;
import srx.open.com.activity.HomeActivity;
import srx.open.com.bean.OnReadedBean;
import srx.open.com.mode.ShowMsgNotifyMode;
import srx.open.com.utils.CommonUtils;
import srx.open.com.utils.Log;
import srx.open.com.utils.OpenToast;
import srx.open.com.utils.UserUtils;

/**
 * 注册消息接收器
 */
public class MyMessageReceiver extends BroadcastReceiver {

    private static final String TAG = "MyMessageReceiver";

    // 事件监听
    public static ArrayList<EventListener> ehList = new ArrayList<EventListener>();

    public static final int NOTIFY_ID = 0x000;
    public static int mNewNum = 0;//
    BmobUserManager userManager;
    BmobChatUser currentUser;

    //如果你想发送自定义格式的消息，请使用sendJsonMessage方法来发送Json格式的字符串，然后你按照格式自己解析并处理

    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        String json = intent.getStringExtra("msg");
        Log.i("收到的message = " + json);
        mContext = context;
        userManager = BmobUserManager.getInstance(context);
        currentUser = userManager.getCurrentUser();
        boolean isNetConnected = CommonUtils.isNetworkAvailable(context);
        if (isNetConnected) { // 解析推送过来的数据.
            parseMessage(context, json);
        } else { // 网络有问题（未连接）.
            for (int i = 0; i < ehList.size(); i++)
                ((EventListener) ehList.get(i)).onNetChange(isNetConnected);
        }
    }

    /**
     * 解析Json字符串
     */
    private void parseMessage(final Context context, String json) {
        JSONObject jo;
        try {
            Log.d(TAG, " parseMessage json:" + json);
            jo = new JSONObject(json);
            String tag = BmobJsonUtil.getString(jo, BmobConstant.PUSH_KEY_TAG);
            if (tag.equals(BmobConfig.TAG_OFFLINE)) {//下线通知
                if (currentUser != null) {
                    if (ehList.size() > 0) {// 有监听的时候，传递下去
                        for (EventListener handler : ehList)
                            handler.onOffline();
                    } else {
                        //清空数据
                        userManager.logout();
                    }
                }
            } else {
                String fromId = BmobJsonUtil.getString(jo, BmobConstant.PUSH_KEY_TARGETID);
                //增加消息接收方的ObjectId--目的是解决多账户登陆同一设备时，无法接收到非当前登陆用户的消息。
                final String toId = BmobJsonUtil.getString(jo, BmobConstant.PUSH_KEY_TOID);
                String msgTime = BmobJsonUtil.getString(jo, BmobConstant.PUSH_READED_MSGTIME);
                if (fromId != null && !BmobDB.create(context, toId).isBlackUser(fromId)) {//该消息发送方不为黑名单用户
                    if (TextUtils.isEmpty(tag)) {//不携带tag标签--此可接收陌生人的消息
                        if (toId.equals(currentUser.getObjectId()) && !fromId.equals(currentUser.getObjectId())) {
                            Log.d(TAG, "创建接收消息... ..." + toId + currentUser.getObjectId());
                            BmobChatManager.getInstance(context).createReceiveMsg(json, new OnReceiveListener() {

                                @Override
                                public void onSuccess(BmobMsg msg) {
                                    if (ehList.size() > 0) {// 有监听的时候，传递下去
                                        for (int i = 0; i < ehList.size(); i++) {
                                            ((EventListener) ehList.get(i)).onMessage(msg);
                                        }
                                    } else {
                                        EventBus.getDefault().post(new ShowMsgNotifyMode("MyMsgFragment.class")); // 刷新界面的来消息提示.
                                        boolean isAllow = true;//CustomApplcation.getInstance().getSpUtil().isAllowPushNotify();
                                        if (isAllow && currentUser != null && currentUser.getObjectId().equals(toId)) {//当前登陆用户存在并且也等于接收方id
                                            mNewNum++; // 未读消息.
                                            showMsgNotify(context, msg);
                                        }
                                    }
                                }

                                @Override
                                public void onFailure(int code, String arg1) {
                                    Log.i("获取接收的消息失败：" + arg1);
                                }
                            });
                        } else {
                            Log.d(TAG, "自己的消息... ..." + toId + " id:" + currentUser.getObjectId() + "  fromId:" + fromId);
                        }
                    } else {//带tag标签
                        if (tag.equals(BmobConfig.TAG_ADD_CONTACT)) {
                            //保存好友请求道本地，并更新后台的未读字段
                            BmobInvitation message = BmobChatManager.getInstance(context).saveReceiveInvite(json, toId);
                            if (currentUser != null) {//有登陆用户
                                if (toId.equals(currentUser.getObjectId())) {
                                    if (ehList.size() > 0) {// 有监听的时候，传递下去
                                        for (EventListener handler : ehList)
                                            handler.onAddUser(message);
                                    } else {
//                                        showOtherNotify(context, message.getFromname(), toId, message.getFromname() + "请求添加好友", NewFriendActivity.class);
                                    }
                                }
                            }
                        } else if (tag.equals(BmobConfig.TAG_ADD_AGREE)) { // 好友同意.
                            String username = BmobJsonUtil.getString(jo, BmobConstant.PUSH_KEY_TARGETUSERNAME);
                            //收到对方的同意请求之后，就得添加对方为好友--已默认添加同意方为好友，并保存到本地好友数据库
                            BmobUserManager.getInstance(context).addContactAfterAgree(username, new FindListener<BmobChatUser>() {

                                @Override
                                public void onError(int arg0, final String arg1) {
                                }

                                @Override
                                public void onSuccess(List<BmobChatUser> arg0) {
                                    //保存到内存中
//                                    CustomApplcation.getInstance().setContactList(CollectionUtils.list2map(BmobDB.create(context).getContactList()));
                                }
                            });
                            //显示通知
//                            showOtherNotify(context, username, toId, username + "同意添加您为好友", MainActivity.class);
                            //创建一个临时验证会话--用于在会话界面形成初始会话
                            BmobMsg.createAndSaveRecentAfterAgree(context, json);

                        } else if (tag.equals(BmobConfig.TAG_READED)) {//已读回执
                            String conversionId = BmobJsonUtil.getString(jo, BmobConstant.PUSH_READED_CONVERSIONID);
                            if (currentUser != null) {
                                //更改某条消息的状态
                                BmobChatManager.getInstance(context).updateMsgStatus(conversionId, msgTime);
                                if (toId.equals(currentUser.getObjectId())) {
//                                    EventBus.getDefault().post(new OnReadedBean()); // eventbus 预留测试.
                                    if (ehList.size() > 0) {// 有监听的时候，传递下去--便于修改界面
                                        for (EventListener handler : ehList)
                                            handler.onReaded(conversionId, msgTime);
                                    }
                                }
                            }
                        }
                    }
                } else {//在黑名单期间所有的消息都应该置为已读，不然等取消黑名单之后又可以查询的到
                    BmobChatManager.getInstance(context).updateMsgReaded(true, fromId, msgTime);
                    Log.i("该消息发送方为黑名单用户");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            //这里截取到的有可能是web后台推送给客户端的消息，也有可能是开发者自定义发送的消息，需要开发者自行解析和处理
            Log.i("parseMessage错误：" + e.getMessage());
        }
    }

    /**
     * 显示与聊天消息的通知
     */
    public void showMsgNotify(Context context, BmobMsg msg) {
        // 更新通知栏
        int icon = R.drawable.ic_launcher;
        String trueMsg = "";
        if (msg.getMsgType() == BmobConfig.TYPE_TEXT && msg.getContent().contains("\\ue")) {
            trueMsg = "[表情]";
        } else if (msg.getMsgType() == BmobConfig.TYPE_IMAGE) {
            trueMsg = "[图片]";
        } else if (msg.getMsgType() == BmobConfig.TYPE_VOICE) {
            trueMsg = "[语音]";
        } else if (msg.getMsgType() == BmobConfig.TYPE_LOCATION) {
            trueMsg = "[位置]";
        } else {
            trueMsg = msg.getContent();
        }
        CharSequence tickerText = msg.getBelongUsername() + ":" + trueMsg;
        String contentTitle = msg.getBelongUsername() + " (" + mNewNum + "条新消息)";
//
        Intent intent = new Intent(context, HomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//
//        boolean isAllowVoice = false;//CustomApplcation.getInstance().getSpUtil().isAllowVoice();
//        boolean isAllowVibrate = false;//CustomApplcation.getInstance().getSpUtil().isAllowVibrate();
//
//        BmobNotifyManager.getInstance(context).showNotifyWithExtras(true, true, icon, tickerText.toString(), contentTitle, tickerText.toString(), null);
        Log.d(TAG, "showMsgNotify(Context con 111111");
    }

    /**
     * 显示其他Tag的通知
     */
    public void showOtherNotify(Context context, String username, String toId, String ticker, Class<?> cls) {
//        boolean isAllow = CustomApplcation.getInstance().getSpUtil().isAllowPushNotify();
//        boolean isAllowVoice = CustomApplcation.getInstance().getSpUtil().isAllowVoice();
//        boolean isAllowVibrate = CustomApplcation.getInstance().getSpUtil().isAllowVibrate();
//        if(isAllow && currentUser!=null && currentUser.getObjectId().equals(toId)){
//            //同时提醒通知
//            BmobNotifyManager.getInstance(context).showNotify(isAllowVoice,isAllowVibrate,R.drawable.ic_launcher, ticker,username, ticker.toString(),NewFriendActivity.class);
//        }
    }

}
